package com.mychat.controller;

import com.mychat.annotation.GlobalInterceptor;
import com.mychat.entity.Constants;
import com.mychat.entity.dto.MessageSendDto;
import com.mychat.entity.dto.TokenUserInfoDto;
import com.mychat.entity.vo.ResponseVO;
import com.mychat.entity.vo.UserInfoVO;
import com.mychat.enums.ResponseCodeEnum;
import com.mychat.exception.BusinessException;
import com.mychat.redis.RedisComponent;
import com.mychat.redis.RedisUtils;
import com.mychat.service.UserInfoService;
import com.mychat.websocket.MessageHandler;
import com.wf.captcha.ArithmeticCaptcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController("accountController")
@RequestMapping("/account")
public class AccountController extends ABaseController {

    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private MessageHandler messageHandler;

    @RequestMapping("/checkCode")
    public ResponseVO checkCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
        String code = captcha.text();
        String checkCodeKey = UUID.randomUUID().toString();
        String checkCodeBase64 = captcha.toBase64();
        Map<String, String> map = new HashMap<>();
        map.put(Constants.CHECK_CODE, checkCodeBase64);
        map.put(Constants.CHECK_CODE_KEY, checkCodeKey);
        redisUtils.setex(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey, code, Constants.REDIS_TIME_1MIN * 10);
        return getSuccessResponseVO(map);
    }

    @RequestMapping("/register")
    public ResponseVO register(@NotEmpty String checkCodeKey, @NotEmpty @Email String email,@NotEmpty String password,@NotEmpty String nickName,@NotEmpty String checkCode) {
        try {
            if(!checkCode.equalsIgnoreCase((String)redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))) {
                throw new BusinessException("图片验证码不正确");
            }
            userInfoService.register(email, nickName, password);
        } catch (BusinessException e) {
            throw new RuntimeException(e);
        } finally {
            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/login")
    public ResponseVO login(@NotEmpty String checkCodeKey, @NotEmpty @Email String email,@NotEmpty String password,@NotEmpty String checkCode) throws BusinessException {
        try {
            if(!checkCode.equalsIgnoreCase((String)redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))) {
                throw new BusinessException("图片验证码不正确");
            }
            UserInfoVO userInfoVO = userInfoService.login(email, password);
            return getSuccessResponseVO(userInfoVO);
        } catch (BusinessException e) {
            throw e;
        } finally {
            redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }
    }

    @GlobalInterceptor
    @RequestMapping("/getSysSetting")
    public ResponseVO getSysSetting() throws BusinessException {
        return getSuccessResponseVO(redisComponent.getSysSetting());
    }

    @RequestMapping("/test")
    public ResponseVO test() {
        MessageSendDto messageSendDto = new MessageSendDto();
        messageSendDto.setMessageContent("测试" + System.currentTimeMillis());
        messageHandler.sendMessage(messageSendDto);
        return getSuccessResponseVO(null);
    }

}
